package com.thekernel.youdaoyun.mapper;

import com.thekernel.youdaoyun.YoudaoyunApplication;
import com.thekernel.youdaoyun.bean.Note;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;

/**
 * @Author TheKernel
 * @Date 2019/9/18 9:42 上午
 * @Version 1.0
 * @Description 测试 NoteMapper
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = YoudaoyunApplication.class)
public class testNoteMapper {

    @Autowired
    private NoteMapper noteMapper;

    @Test
    public void testAddNote() {
        Note note = new Note();
        note.setNoteTitle("测试Note");
        note.setNoteContent("this is just a test");
        note.setCategory(1);
        note.setNoteUser(1);
        note.setStatus(Boolean.TRUE);
        note.setCreateTime(new Date());

        int result = this.noteMapper.add(note);
        Assert.assertEquals(1, result);
    }

    @Test
    public void testAddInvalidNote() {
        Note note = new Note();
        note.setNoteTitle("测试InvalidNote");
        note.setNoteContent("this is just a invalid test");
        note.setCategory(1);
        note.setNoteUser(1);
        note.setStatus(Boolean.FALSE);
        note.setCreateTime(new Date());

        int result = this.noteMapper.add(note);
        Assert.assertEquals(1, result);
    }

    @Test
    public void testQueryAllValidNotes() {
        long userId = 1;  // 测试用户
        Note[] notes = this.noteMapper.queryAllValidNotesByUserId(userId);

        for (Note note: notes) {
            System.out.println(note);
        }
    }

    @Test
    public void testQueryAllDeletedNotes() {
        long userId = 1;  // 测试用户
        Note[] notes = this.noteMapper.queryAllDeletedNotesByUserId(userId);

        for (Note note: notes) {
            System.out.println(note);
        }
    }

    @Test
    public void testDeleteDeletedNote() {
        long noteId = 2;  // 测试noteId
        int result = this.noteMapper.deleteDeletedNoteByNoteId(noteId);
        Assert.assertEquals(1, result);
    }

    @Test
    public void testDeleteNoteByNoteId() {
        long noteId = 1;  // 测试 noteId
        int result = this.noteMapper.deleteNoteByNoteId(noteId);
        Assert.assertEquals(1, result);
    }

    @Test
    public void testRecoverDeletedNote() {
        long noteId = 1;
        int result = this.noteMapper.recoverDeletedNotes(noteId);
        Assert.assertEquals(1, result);
    }

    @Test
    public void testQueryNoteByNoteId() {
        long noteId = 1;
        Note note = this.noteMapper.queryNoteByNoteId(noteId);
        System.out.println(note);
    }

    @Test
    public void testUpdateNote() {
        long noteId = 1;  // 测试笔记 id
        Note note = this.noteMapper.queryNoteByNoteId(noteId);
        note.setNoteTitle("test update");

        int result = this.noteMapper.updateNote(note);
        Assert.assertEquals(0, result);
    }

}
